package com.ht.managermentserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.ht.managermentserver.entity.SysDic;
import com.ht.managermentserver.entity.SysDicType;
import com.ht.managermentserver.service.SysDicService;
import com.ht.managermentserver.service.SysDicTypeService;
import com.ht.managermentserver.sysenum.ErrorCode;
import com.ht.managermentserver.utils.JsonResult;
import io.swagger.annotations.*;
import org.assertj.core.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

/**
 * 字典集合 前端控制器
 *
 * @author czy
 * @since 2020-06-04
 */
@RestController
@Api(tags = "字典集合")
@RequestMapping("/sysDic")
public class SysDicController extends BaseController {

  @Autowired SysDicService sysDicService;

  @Autowired SysDicTypeService sysDicTypeService;

  @PostMapping("/getSysDicBypage")
  @ApiOperation("字典分页")
  @ApiImplicitParams(@ApiImplicitParam(name = "page",value = "页码"))
  public JsonResult<IPage<SysDic>> getSysDicPage(
      @RequestParam int page,
      @RequestParam(defaultValue = "20") int pageSize,
      @RequestParam(defaultValue = "0") int dicType,
      @RequestParam(defaultValue = "") String dicKeyValue) {

    Page<SysDic> page_gen = new Page<>();
    page_gen.setCurrent(page);
    page_gen.setSize(pageSize);

    QueryWrapper queryWrapper = new QueryWrapper();

    if (!Strings.isNullOrEmpty(dicKeyValue)) {
      queryWrapper.like("sysdic."+SysDic.DIC_KEY_VALUE, dicKeyValue);
    }
    if (dicType != 0) {
      queryWrapper.eq("sysdic."+SysDic.DIC_TYPE, dicType);
    }

    IPage<SysDic> page_list = sysDicService.getSysDicByPage(page_gen, queryWrapper);

    return JsonResult.createSuccess(page_list);
  }

  @GetMapping("/getAllDictype")
  @ApiOperation("获取所有的字典类型")
  public JsonResult<List<SysDicType>> getAllDictype() {
    List<SysDicType> sysDicTypes = sysDicTypeService.list();
    return JsonResult.createSuccess(sysDicTypes);
  }

  @PostMapping("/getDicById")
  @ApiOperation("根据id 获取字典")
  public JsonResult<SysDic> getSysDicById(@RequestParam Long id) {
    SysDic sysDic = sysDicService.getById(id);
    return JsonResult.createSuccess(sysDic);
  }

  @PostMapping("/getDicTypeById")
  @ApiOperation("根据id 获取字典类型")
  public JsonResult<SysDicType> getDicTypeById(@RequestParam Integer id) {
    SysDicType dictype = sysDicTypeService.getById(id);
    return JsonResult.createSuccess(dictype);
  }

  /* @ModelAttribute 将json转为bean对象 */
  @PostMapping("/saveDic")
  @ApiOperation(value = "保存字典数据")
  public JsonResult saveDic(@ModelAttribute SysDic sysDic) {

    if (Strings.isNullOrEmpty(sysDic.getDicKeyCode())) {

      return JsonResult.createError("数据编码不能为空");
    }
    if (Strings.isNullOrEmpty(sysDic.getDicKeyValue())) {

      return JsonResult.createError("数据名称不能为空");
    }
    if (sysDic.getDicType()==0) {
      return JsonResult.createError("请选择一种数据类型");
    }

    if (sysDicService.saveOrUpdate(sysDic)) {
      return JsonResult.create("保存成功", ErrorCode.SUCCESS);
    }
    return JsonResult.createError("保存失败");
  }

  @PostMapping("/saveDicType")
  @ApiOperation(value = "保存字典类型数据")
  public JsonResult saveDicType(@ModelAttribute SysDicType sysDicType) {

    if (Strings.isNullOrEmpty(sysDicType.getDicType())) {

      return JsonResult.createError("字典类型不能为空");
    }
    if (sysDicTypeService.saveOrUpdate(sysDicType)) {
      return JsonResult.create("保存成功", ErrorCode.SUCCESS);
    }
    return JsonResult.createError("保存失败");
  }

  @ApiOperation(value = "删除字典")
  @PostMapping("/delDicById")
  public JsonResult delDicById(@RequestParam Long id){
    if (sysDicService.removeById(id)) {
      return JsonResult.createSuccess(true);
    }
    return JsonResult.createError("删除失败");
  }

  @ApiOperation(value = "删除字典类型")
  @PostMapping("/delDicTypeById")
  public JsonResult delDicTypeById(@RequestParam Integer id){
    if (sysDicTypeService.removeById(id)) {
      return JsonResult.createSuccess(true);
    }
    return JsonResult.createError("删除失败");
  }

}
